﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;


namespace application.control
{
    public class gridInventory : common.controls.baseDataGridView 
    {
        public gridInventory()
        {
            myItemSource.DataSource = DataAccess.DataCache.myMasterDS.Item;
            this.AutoGenerateColumns = false;
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
        }
        BindingSource myItemSource = new BindingSource();
        public enum ColumnNames
        {
            onDate,houseCode, itemCode,itemSubCode,itemName, itemUnit, qty, eopQty
        };
        public void Init(data.tmpDS.inventoryDataTable tbl)
        {
            Init(tbl, null);
        }
        public void Init(data.tmpDS.inventoryDataTable tbl, string filter)
        {
            // 
            // onDate
            // 
            DataGridViewCellStyle dateCellType = new DataGridViewCellStyle();
            dateCellType.Alignment =  System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dateCellType.Format = "d"; 

            common.controls.DataGridViewTextBoxColumnExt onDate = new common.controls.DataGridViewTextBoxColumnExt();
            onDate.DataPropertyName = tbl.onDateColumn.ColumnName;
            onDate.HeaderText = "Ngày";
            onDate.Name = ColumnNames.onDate.ToString();
            onDate.Width = 85;
            onDate.DefaultCellStyle = dateCellType;

            // 
            // houseCode
            // 
            common.controls.DataGridViewTextBoxColumnExt houseCode = new common.controls.DataGridViewTextBoxColumnExt();
            houseCode.Uppercase = true;
            houseCode.DataPropertyName = tbl.houseCodeColumn.ColumnName;
            houseCode.HeaderText = "Kho";
            houseCode.Name = ColumnNames.houseCode.ToString();
            houseCode.Width = 65;

            // 
            // itemCode
            // 
            common.controls.DataGridViewTextBoxColumnExt itemCode = new common.controls.DataGridViewTextBoxColumnExt();
            itemCode.Uppercase = true;
            itemCode.DataPropertyName = tbl.itemCodeColumn.ColumnName;
            itemCode.HeaderText = "Mã hàng";
            itemCode.Name = ColumnNames.itemCode.ToString();
            itemCode.Width = 100;

            // 
            // itemSubCode
            // 
            common.controls.DataGridViewTextBoxColumnExt itemSubCode = new common.controls.DataGridViewTextBoxColumnExt();
            itemSubCode.Uppercase = true;
            itemSubCode.DataPropertyName = tbl.itemSubCodeColumn.ColumnName;
            itemSubCode.HeaderText = "";
            itemSubCode.Name = ColumnNames.itemSubCode.ToString();
            itemSubCode.Width = 35;

            // 
            // itemName
            // 
            DataGridViewComboBoxColumn itemName = new DataGridViewComboBoxColumn();
            itemName.DataSource = this.myItemSource;
            itemName.DataPropertyName = tbl.itemCodeColumn.ColumnName;
            itemName.DisplayMember = DataAccess.DataCache.myMasterDS.Item.nameColumn.ColumnName;
            itemName.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemName.HeaderText = "Diễn giải";
            itemName.Name = ColumnNames.itemName.ToString();
            itemName.ReadOnly = true;

            itemName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemName.Width = 370;
            itemName.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemName.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;

            // 
            // unit
            // 
            DataGridViewComboBoxColumn itemUnit = new DataGridViewComboBoxColumn();
            itemUnit.DataSource = this.myItemSource;
            itemUnit.DataPropertyName = tbl.itemCodeColumn.ColumnName;
            itemUnit.DisplayMember = DataAccess.DataCache.myMasterDS.Item.unitColumn.ColumnName;
            itemUnit.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemUnit.HeaderText = "ĐVT";
            itemUnit.Name = ColumnNames.itemUnit.ToString();
            itemUnit.ReadOnly = true;
            itemUnit.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemUnit.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemUnit.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;
            itemUnit.Width = 60;
            // 
            // Qty
            // 
            DataGridViewCellStyle qtyCellType = new DataGridViewCellStyle();
            qtyCellType.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            qtyCellType.Format = "N2"; qtyCellType.NullValue = null;

            DataGridViewTextBoxColumn qty = new DataGridViewTextBoxColumn();
            qty.DataPropertyName = tbl.qtyColumn.ColumnName;
            qty.HeaderText = "Tồn.ngày";
            qty.Name = ColumnNames.qty.ToString();
            qty.DefaultCellStyle = qtyCellType;
            qty.Width = 80;

            DataGridViewTextBoxColumn eopQty = new DataGridViewTextBoxColumn();
            eopQty.DataPropertyName = tbl.eopQtyColumn.ColumnName;
            eopQty.HeaderText = "Tồn.cuối";
            eopQty.Name = ColumnNames.eopQty.ToString();
            eopQty.DefaultCellStyle = qtyCellType;
            eopQty.Width = 80;
            // 
            // myDetailGrid
            // 
            this.Columns.Clear();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { onDate,houseCode,itemCode,itemSubCode,itemName,itemUnit,qty,eopQty});

            DataView dataView = new DataView(tbl);
            dataView.RowFilter = (filter == null ? "" : filter);
            this.DataSource = dataView;
            
            UpdateItemList();
            AutoArrange();
        }
        public new int RowCount
        {
            get
            {
                if (this.DataSource == null) return 0;
                return ((DataView)this.DataSource).Count;
            }
        }
        private void UpdateItemList()
        {
            DataView dataView = (DataView)this.DataSource;
            for (int idx = 0; idx < dataView.Count; idx++)
            { 
                DataAccess.DataCache.FindITEM( ((data.tmpDS.inventoryRow)(dataView[idx] as DataRowView).Row).itemCode); 
            }
        }
    }
}
